package response

import (
	"github.com/gogf/gf/v2/net/ghttp"
)

type ResultCode struct {
}
type statusCode int

const sucCode statusCode = 0
const comCode statusCode = 200
const errCode statusCode = 500
const forbiddenCode statusCode = 403

type JsonResult struct {
	Code     statusCode  `json:"code"`     // 响应编码：0成功 401请登录 403无权限 500错误
	Msg      string      `json:"msg"`      // 消息提示语
	Data     interface{} `json:"data"`     // 数据对象
	Redirect string      `json:"redirect"` //跳转链接
	Count    int         `json:"count"`    //总数

}

//ApiResp 通用API响应
type ApiResp struct {
	c *JsonResult
	r *ghttp.Request
}

//CommonResp 返回一个成功的消息体
func CommonResp(r *ghttp.Request) *ApiResp {
	msg := JsonResult{
		Code:     comCode,
		Msg:      "操作完成",
		Data:     nil,
		Redirect: "",
		Count:    0,
	}
	var a = ApiResp{
		c: &msg,
		r: r,
	}
	return &a
}

//SuccessResp 返回一个成功的消息体
func SuccessResp(r *ghttp.Request) *ApiResp {
	msg := JsonResult{
		Code: sucCode,
		Msg:  "操作成功",
		Data: nil,
	}
	var a = ApiResp{
		c: &msg,
		r: r,
	}
	return &a
}

//ErrorResp 返回一个错误的消息体
func ErrorResp(r *ghttp.Request) *ApiResp {
	msg := JsonResult{
		Code: errCode,
		Msg:  "操作失败",
	}
	var a = ApiResp{
		c: &msg,
		r: r,
	}
	return &a
}

//ForbiddenResp 返回一个拒绝访问的消息体
func ForbiddenResp(r *ghttp.Request) *ApiResp {
	msg := JsonResult{
		Code: forbiddenCode,
		Msg:  "无操作权限",
	}
	var a = ApiResp{
		c: &msg,
		r: r,
	}
	return &a
}

// SetMsg 设置消息体的内容
func (resp *ApiResp) SetMsg(msg string) *ApiResp {
	resp.c.Msg = msg
	return resp
}

// SetData 设置消息体的数据
func (resp *ApiResp) SetData(data interface{}) *ApiResp {
	resp.c.Data = data
	return resp
}

// SetCount 设置消息体的数据
func (resp *ApiResp) SetCount(count int) *ApiResp {
	resp.c.Count = count
	return resp
}

// SetErrorCode 设置消息体的编码
func (resp *ApiResp) SetErrorCode() *ApiResp {
	resp.c.Code = errCode
	return resp
}

// SetSuccessCode 设置消息体的编码
func (resp *ApiResp) SetSuccessCode() *ApiResp {
	resp.c.Code = sucCode
	return resp
}

// SetForbiddenCode 设置消息体的编码
func (resp *ApiResp) SetForbiddenCode() *ApiResp {
	resp.c.Code = forbiddenCode
	return resp
}

// SetError 设置所有消息
func (resp *ApiResp) SetError(msg string) *ApiResp {
	resp.c.Msg = msg
	resp.c.Code = errCode
	return resp
}
func (resp *ApiResp) SetErrorJsonExit(msg string) {
	resp.c.Msg = msg
	resp.c.Code = errCode
	resp.r.Response.WriteJsonExit(resp.c)
}

// SetForbidden 设置所有消息
func (resp *ApiResp) SetForbidden(msg string) *ApiResp {
	resp.c.Msg = msg
	resp.c.Code = forbiddenCode
	return resp
}

func (resp *ApiResp) SetForbiddenJsonExit(msg string) {
	resp.c.Msg = msg
	resp.c.Code = forbiddenCode
	resp.r.Response.WriteJsonExit(resp.c)
}

// SetSuccess 设置所有消息
func (resp *ApiResp) SetSuccess(msg string, data interface{}) *ApiResp {
	resp.c.Msg = msg
	resp.c.Code = sucCode
	resp.c.Data = data
	return resp
}

func (resp *ApiResp) SetSuccessJsonExit(msg string, data interface{}) {
	resp.c.Msg = msg
	resp.c.Code = sucCode
	resp.c.Data = data
	resp.r.Response.WriteJsonExit(resp.c)
}
func (resp *ApiResp) SetSuccessMapExit(data JsonResult) {
	data.Code = sucCode
	resp.r.Response.WriteJsonExit(data)
}

// WriteJson 输出json到客户端
func (resp *ApiResp) WriteJson() {
	resp.r.Response.WriteJson(resp.c)
}
func (resp *ApiResp) WriteJsonExit() {
	resp.r.Response.WriteJsonExit(resp.c)
}
